﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Luna.WPF.ApplicationFramework;
using Caliburn.Core.Metadata;
using System.ComponentModel;
using Caliburn.PresentationFramework;
using XWord.Fundamental.Domain;
using XWord.WPF.ApplicationFramework;
using XWord.GUI.Presenters.Interface;
namespace XWord.GUI.Presenters.Fundamental
{
    //[Singleton(typeof(IEtymaCategoryExplorerPresenter))]
    public class EtymaCategoryExplorerPresenter : DockingPresenter, IEtymaCategoryExplorerPresenter
    {
        public EtymaCategoryExplorerPresenter()
        {
            this.DisplayName = "分类词根词缀";
        }

        protected override void OnInitialize()
        {

            EtymaLibraryPresenter.PropertyChanged += new PropertyChangedEventHandler(EtymaLibraryExplorerPresenter_PropertyChanged);
            base.OnInitialize();
        }

        void EtymaLibraryExplorerPresenter_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "AllEtymaLibraryList")
            {
                var list = EtymaLibraryPresenter.AllEtymaLibraryList;
                PrefixEtymaList = list.Where(l => l.EtymaType == EtymaType.PreFix).ToObservableCollection();
                SuffixEtymaList = list.Where(l => l.EtymaType == EtymaType.Suffix).ToObservableCollection();
                EtymonList = list.Where(l => l.EtymaType == EtymaType.Etymon).ToObservableCollection();
                //GetPreFix();
                //GetSuffix();
                //GetEtymonList();
                EtymaLibraryPresenter.PropertyChanged -= EtymaLibraryExplorerPresenter_PropertyChanged;
            }
        }

        private List<EtymaMessage> _etymaMessageList;

        private void GetEtymaMessage()
        {
            if (_etymaMessageList == null)
            {
                _etymaMessageList = new List<EtymaMessage>();
                var list = EtymaLibraryPresenter.AllEtymaLibraryList;
                foreach (var item in list)
                {
                    _etymaMessageList.AddRange(EtymaUtil.ParseText(item.Name));
                }
            }
        }

        //public void GetPreFix()
        //{
        //    GetEtymaMessage();
        //    PrefixEtymaList=_etymaMessageList.Where(e => (e.EtymaType & EtymaType.PreFix) == EtymaType.PreFix).ToObservableCollection();
        //}

        //public void GetSuffix()
        //{
        //    GetEtymaMessage();
        //    SuffixEtymaList = _etymaMessageList.Where(e => (e.EtymaType & EtymaType.Suffix) == EtymaType.Suffix).ToObservableCollection();
        //}

        //public void GetEtymonList()
        //{
        //    GetEtymaMessage();
        //    EtymonList = _etymaMessageList.Where(e => (e.EtymaType & EtymaType.Etymon) == EtymaType.Etymon).ToObservableCollection();
        //}

        public IObservableCollection<EtymaLibrary> _prefixEtymaList;
        public IObservableCollection<EtymaLibrary> PrefixEtymaList
        {
            get { return _prefixEtymaList ?? (_prefixEtymaList = new BindableCollection<EtymaLibrary>()); }
            set
            {
                _prefixEtymaList = value;
                NotifyOfPropertyChange("PrefixEtymaList");
            }
        }

        public IObservableCollection<EtymaLibrary> _etymonList;
        public IObservableCollection<EtymaLibrary> EtymonList
        {
            get { return _etymonList ?? (_etymonList = new BindableCollection<EtymaLibrary>()); }
            set
            {
                _etymonList = value;
                NotifyOfPropertyChange("EtymonList");
            }
        }

        public IObservableCollection<EtymaLibrary> _suffixEtymaList;
        public IObservableCollection<EtymaLibrary> SuffixEtymaList
        {
            get { return _suffixEtymaList ?? (_suffixEtymaList = new BindableCollection<EtymaLibrary>()); }
            set
            {
                _suffixEtymaList = value;
                NotifyOfPropertyChange("SuffixEtymaList");
            }
        }

        public IEtymaLibraryExplorerPresenter EtymaLibraryPresenter { get; set; }
    }
}
